package ar.uba.dc.cmovil.tripplanner.view;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.util.HashMap;
import java.util.Map;

import ar.uba.dc.cmovil.tripplanner.R;
import ar.uba.dc.cmovil.tripplanner.activity.ViewTripsActivity;
import ar.uba.dc.cmovil.tripplanner.domain.Trip;
import ar.uba.dc.cmovil.tripplanner.widget.TripItemWidget;

/**
 * Created by anton on 9/28/13.
 */
public class TripsView extends View {

    private Activity activity;
    private ViewGroup tripsContainer;
    private Button newTripButton;
    private Map<Trip, TripItemWidget> tripTripItemWidgetMap;

    public TripsView(Activity activity) {
        super(activity, null);

        this.activity = activity;
        this.tripTripItemWidgetMap = new HashMap<Trip, TripItemWidget>();

        activity.setContentView(R.layout.trips_view);
        tripsContainer = (ViewGroup) activity.findViewById(R.id.trips_container);

        newTripButton = (Button) activity.findViewById(R.id.new_trip_button);
    }

    public Button newTripButton() {
        return newTripButton;
    }

    public void addTrip(Trip trip) {
        TripItemWidget tripView = new TripItemWidget(activity.getApplicationContext(), trip);
        tripView.setTripEventsHandler((ViewTripsActivity) activity);

        this.tripTripItemWidgetMap.put(trip, tripView);
        this.tripsContainer.addView(tripView);
    }

    public void removeTrip(Trip trip) {
        if (!this.tripTripItemWidgetMap.containsKey(trip)) {
            return;
        }

        TripItemWidget tripView = this.tripTripItemWidgetMap.get(trip);

        this.tripsContainer.removeView(tripView);
    }
}
